var app = getApp();
Page({
    data:{
		siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
        inited : false,
        value : '',
        postdata : {},
        options : {},
        user : {},
        card : {},
    },
    onLoad:function(options){
        var self = this;
        this.data.options = options;

        wx.setNavigationBarTitle({
          title: '核销卡包',
        });
        app.com.auth(function(user){
            if( options.scene ){
                var postdata = {
                    op : 'hexiaokabaostart',
                };
                if( options.scene ){
                    var list = options.scene.split('_');
                    postdata.cid = list[0] ? list[0] : 0;
                    postdata.type = list[1] ? list[1] : 0;
                }
                self.setData({
                    postdata : postdata,
                })

                app.com.http('admin','POST',postdata,0,false,'','',function(res){
                    if (!res.data.errno && res.data ) {
                        self.setData({
                            card : res.data.data.card,
                            user : res.data.data.user,
                            inited : true,
                        })
                    }else{
                        app.com.toast( res.data.message );
                    }
                });
            }else{
                app.com.toast('请重新扫码');return false;
            }
        })
    },
    onPullDownRefresh: function () {
        if( this.data.isdown ) return false;
        this.setData({
            isdown : true,
        });
        
        this.onLoad( this.data.options );
        this.setData({
            isdown : false,
        });
        wx.stopPullDownRefresh();
    },
    inputvalue(e){
        var self = this;
        self.setData({
            value : e.detail
        })
    },
    confirm(){
        var self = this;
        if( !self.data.postdata.cid ) {
            app.com.toast('请重新扫码');return false;
        }
        var postdata = {
            op : 'hexiaokabao',
            cid : self.data.postdata.cid,
            type : self.data.postdata.type,
            levelid : self.data.postdata.levelid,
            value : self.data.value,
        };
        app.com.confirm('确定提交吗？',function(){
            app.com.http('admin','POST',postdata,0,true,'','',function(res){
                if (!res.data.errno) {
                    app.com.alert(res.data.message,function(){
                    })

                    if(postdata.type == 1){
                        self.setData({
                            'card.money' : res.data.data.last,
                        })
                    }else{
                        self.setData({
                            'card.hxtimes' : res.data.data.last,
                        })
                    }
                    
                }else{
                    app.com.toast( res.data.message );
                }
            });
        })
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    scan(e){
        wx.scanCode({
            success : function(res){
                if( res.path ){
                    var path =  res.path.indexOf("/") == 0 ? res.path : '/'+res.path;
                    wx.navigateTo({
                      url: path,
                    });
                }
            }
        });
    }
})